package Util;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import messagePacket.Packet;
import server.StartServer;

public class TaskQueue {
	// storage the message packet in the queue
	public static Queue<Task> queue = new LinkedBlockingDeque<Task>(10);
	
	private static Lock lock = new ReentrantLock();
	private static Condition condition = lock.newCondition();
	
	public static void inQueue(Task task) {
		lock.lock();
		queue.add(task);
		System.out.println("add task in queue");
		lock.unlock();
	}
	public static Task outQueue() throws Exception {
		try {
			lock.lock();
			// judge the queue is null  
			while(queue.isEmpty()) {
				condition.await(); // if the queue not task, need wait 
			}
			Task task = queue.remove();
			System.out.println("get task from queue");
			return task;
		}catch (InterruptedException e) {
			throw new Exception("");
		}
		finally {
			lock.unlock();
		}
	}
}
